# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=psycopg2
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2.9.10
pkgrel=2
pkgdesc="A PostgreSQL database adapter for the Python programming language (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.psycopg.org/'
msys2_references=(
  'purl: pkg:pypi/psycopg2'
)
license=('spdx:LGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-postgresql")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-python-build"
  "${MINGW_PACKAGE_PREFIX}-python-installer"
  "${MINGW_PACKAGE_PREFIX}-python-setuptools"
  "${MINGW_PACKAGE_PREFIX}-cc"
)
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        mingw-fix.patch)
sha256sums=('12ec0b40b0273f95296233e8750441339298e6a572f7039da5b260e3c8b60e11'
            '732fa9437aec1b5ce5569951dc689578d0e4fc83a042e6bf07455cff6041c2e3')
validpgpkeys=('8AD609956CF1899418E19A856013BD3AFCF957DE')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg mingw-fix.patch
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m pytest
}

package() {
  cd "${srcdir}"/python-build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"
}
